package com.moonshineproject.plugin.actionscript.as3project.importer
{
	import com.moonshineproject.plugin.actionscript.as3project.vo.AS3ProjectVO;
	import com.moonshineproject.utils.Utils;
	
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class FlashBuilderImporter extends EventDispatcher
	{
		public static function test(file:File):Boolean
		{
			var ret:Boolean = true;
			
			if (file.resolvePath(".actionScriptProperties").exists == false) 
				ret = false;
			if (file.resolvePath(".project").exists == false)
				ret = false;
			
			return ret;
		}
		
		public static function parse(file:File):AS3ProjectVO
		{
			var p:AS3ProjectVO = new AS3ProjectVO(file);
			
			var projectSettings:File = file.resolvePath(".project");
			readProjectSettings(projectSettings, p);
			
			var actionscriptProperties:File = file.resolvePath(".actionScriptProperties");
			readActionScriptSettings(actionscriptProperties, p);
			
			// For AIR projects we need to meddle with the projectname-app.xml file
			if (p.air == true)
			{
				if (p.targets.length > 0)
				{
					var targetApp:File = p.targets[0];
					var appConfig:File = targetApp.parent.resolvePath(p.projectName+"-app.xml");
					updateAppConfigXML(appConfig, p);
				}
			}
			
			return p;
		}
		
		protected static function readProjectSettings(file:File, p:AS3ProjectVO):void
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var data:XML = XML(stream.readUTFBytes(file.size));
			stream.close();
			
			p.projectName = data.name;
		}
		
		protected static function readActionScriptSettings(file:File, p:AS3ProjectVO):void
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var data:XML = XML(stream.readUTFBytes(file.size));
			stream.close();
			
			var target:File = p.folder.resolvePath(data.compiler.@sourceFolderPath + "/" + data.@mainApplicationPath); 
			p.targets.push(target);
			
			var additional:String = data.compiler.@additionalCompilerArguments;
			// FB seems to keep it as -switch value, while mxmlc takes -switch=value
			additional = additional.replace(/\s+/g,",").replace(/-([^,]+),([^-]+)/g,"-$1=$2");
			p.buildOptions.additional = additional;
			
			p.buildOptions.warnings = Utils.deserializeBoolean(data.compiler.@warn);
			p.buildOptions.accessible = Utils.deserializeBoolean(data.compiler.@generateAccessible);
			p.buildOptions.strict = Utils.deserializeBoolean(data.compiler.@strict);
			p.air = Utils.deserializeBoolean(data.compiler.@useApolloConfig); 
			
			// FB doesn't seem to have a notion of output filename, so we guesstimate it
			p.swfOutput.path = p.folder.resolvePath(data.compiler.@outputFolderPath + "/" + p.projectName + ".swf");
			
			var classPath:File = p.folder.resolvePath(data.compiler.@sourceFolderPath);
			p.classpaths.push(classPath);
		}
		
		protected static function updateAppConfigXML(file:File, p:AS3ProjectVO):void
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var data:String = stream.readUTFBytes(file.size).toString();
			stream.close();
			
			var replacement:String = p.projectName + ".swf";
			
			var flashBuilder:String = "[This value will be overwritten by Flash Builder in the output app.xml]";
			var flexBuilder:String = "[This value will be overwritten by Flex Builder in the output app.xml]";
			
			
			// Try to not mess up the formatting of the XML first
			//  by just string replacing
			if (data.indexOf(flashBuilder) > -1)
			{
				data = data.replace(flashBuilder, replacement);
			}
			else if (data.indexOf(flexBuilder) > -1)
			{
				data = data.replace(flexBuilder, replacement);
			}
			// If that fails we change up the XML
			else
			{
				XML.ignoreComments = false;
				XML.ignoreWhitespace = false;
			
				var dataXML:XML = XML(data);

				var ns:Namespace = dataXML.namespaceDeclarations()[0];
				dataXML.ns::initialWindow.ns::content = replacement;
			
				data = dataXML.toXMLString();
			
				XML.ignoreComments = true;
				XML.ignoreWhitespace = true;				
			}
						
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(data);
			stream.close();
		}
		
	}
}